package com.example.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.constant.WareConstant;
import com.example.common.constant.ware.PurchaseConstant;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;
import com.example.common.vo.ware.MergeVO;
import com.example.common.vo.ware.PurchaseDoneVO;
import com.example.common.vo.ware.PurchaseItemDoneVO;
import com.example.ware.dao.PurchaseDao;
import com.example.ware.entity.PurchaseDetailEntity;
import com.example.ware.entity.PurchaseEntity;
import com.example.ware.service.PurchaseDetailService;
import com.example.ware.service.PurchaseService;
import com.example.ware.service.WareSkuService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {


    @Autowired
    PurchaseDetailService purchaseDetailService;

    @Autowired
    WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<PurchaseEntity> wrapper = new LambdaQueryWrapper<>();
        String key =(String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.eq(PurchaseEntity::getId,key);
        }
        String status =(String) params.get("status");
        if (!StringUtils.isEmpty(status)){
            wrapper.eq(PurchaseEntity::getStatus,status);
        }
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getUnreceive(Map<String, Object> params) {
        LambdaQueryWrapper<PurchaseEntity> wrapper = new LambdaQueryWrapper<>();


        wrapper.eq(PurchaseEntity::getStatus,"0").or().eq(PurchaseEntity::getStatus,"1");

        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),wrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void getMerge(MergeVO mergeVO) {
        Long purchaseId = mergeVO.getPurchaseId();
        if (purchaseId == null){
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(PurchaseConstant.PurchaseStatusEnum.CREATED.getCode());
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();
        }
        List<Long> items = mergeVO.getItems();
        Long finaPurchaseId=purchaseId;
        List<PurchaseDetailEntity> collect = items.stream().map(item -> {
            PurchaseDetailEntity purchaseEntity = new PurchaseDetailEntity();
            purchaseEntity.setId(item);
            purchaseEntity.setStatus(PurchaseConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            purchaseEntity.setPurchaseId(finaPurchaseId);
            return purchaseEntity;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(collect);
    }

    @Override
    public void received(List<Long> ids) {
        List<PurchaseEntity> collect = ids.stream().map(id -> {
            PurchaseEntity byId = this.getById(id);
            return byId;
        }).filter(item -> {
            if (item.getStatus() == WareConstant.PurchaseStatusEnum.CREATED.getCode() ||
                    item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode()){
                return true;
            }
            return false;
        }).map(item->{
            item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
            return item;
        }).collect(Collectors.toList());

        //修改领取单的状态
        this.updateBatchById(collect);

        //获取采购项的id
        collect.forEach(item ->{
           List<PurchaseDetailEntity> purchaseDetails = purchaseDetailService.listDetailBypurchaseId(item.getId());
            List<PurchaseDetailEntity> collect1 = purchaseDetails.stream().map(entity -> {
                PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
                purchaseDetailEntity.setId(entity.getId());
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
                return purchaseDetailEntity;
            }).collect(Collectors.toList());

            purchaseDetailService.updateBatchById(collect1);
        });
    }

    @Override
    public void finish(PurchaseDoneVO doneVO) {
        Long id = doneVO.getId();

        //改变采购项的状态
        Boolean flag= true;
        List<PurchaseItemDoneVO> items = doneVO.getItems();
        List<PurchaseDetailEntity> detailEntitie = new ArrayList<>();
        for (PurchaseItemDoneVO item:items) {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            if (item.getStatus()==WareConstant.PurchaseStatusEnum.HASERROR.getCode()){
                flag = false;
                purchaseDetailEntity.setStatus(item.getStatus());
            }else {
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());
                PurchaseDetailEntity entity = purchaseDetailService.getById(item.getItemId());
                //将成功采购的入库
                wareSkuService.addStock(entity.getSkuId(),entity.getWareId(),entity.getSkuNum());
            }
            purchaseDetailEntity.setId(item.getItemId());
            detailEntitie.add(purchaseDetailEntity);
            purchaseDetailService.updateBatchById(detailEntitie);
        }

        //改变采购单的状态
        PurchaseEntity purchase = new PurchaseEntity();
        purchase.setId(id);
        purchase.setStatus(flag?WareConstant.PurchaseDetailStatusEnum.FINISH.getCode():WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode());
        this.updateById(purchase);

    }

}
